//
//  MengwTableViewController.m
//  PetTown
//
//  Created by lanou3g on 15/6/4.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MengwTableViewController.h"

@interface MengwTableViewController ()
{
    MBProgressHUD * HUD;
}
@end

@implementation MengwTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allDataArray = [[NSMutableArray alloc] init];
    _a = 1;
    //下拉刷新
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //上啦刷新
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    // 注册需要的使用的cell
    [self.tableView registerNib:[UINib nibWithNibName:@"MPTableViewCell" bundle:nil] forCellReuseIdentifier:@"mengCell"];

    
    
    NSArray * rrr = [NSArray arrayWithObjects:@"周榜",@"月榜",@"季榜", nil];
    self.mengChange = [[UISegmentedControl alloc] initWithItems:rrr];
    _mengChange.selectedSegmentIndex = 0;
    _mengChange.frame = CGRectMake(0, 15, 150, 25);
    
    [_mengChange addTarget:self action:@selector(mengChangeView:) forControlEvents:UIControlEventValueChanged];
    
    self.navigationItem.titleView = _mengChange;
    [self JSOn];
}
//下拉刷新
- (void)headerRereshing{
    
    [self.tableView headerEndRefreshing];
}
//上拉刷
-(void)footerRereshing{
    _a++;
    if (_mengChange.selectedSegmentIndex == 0) {
        _uuu = [NSString stringWithFormat:PetRankingQI,[NSString stringWithFormat:@"%d",_a]];
    }else if (_mengChange.selectedSegmentIndex == 1){
        _uuu = [NSString stringWithFormat:PetRankingSS,[NSString stringWithFormat:@"%d",_a]];
    }else if (_mengChange.selectedSegmentIndex == 2){
        _uuu = [NSString stringWithFormat:PetRankingJS,[NSString stringWithFormat:@"%d",_a]];
    }
    [self JSOn];
    [self.tableView footerEndRefreshing];
}
- (void)mengChangeView:(UISegmentedControl *)ss{
    switch (ss.selectedSegmentIndex) {
        case 0:{
            _a = 1;
            [_allDataArray removeAllObjects];
            _uuu = [NSString stringWithFormat:PetRankingQI,[NSString stringWithFormat:@"%d",_a]];
            [self JSOn];
            ;
        }
            break;
        case 1:{
            _a = 1;
            [_allDataArray removeAllObjects];
            _uuu = [NSString stringWithFormat:PetRankingSS,[NSString stringWithFormat:@"%d",_a]];
            [self JSOn];

        }
            break;
        case 2:{
            _a = 1;
            [_allDataArray removeAllObjects];
            _uuu = [NSString stringWithFormat:PetRankingJS,[NSString stringWithFormat:@"%d",_a]];
            [self JSOn];
        }
            break;
        default:
            break;
    }
}
#pragma mark----数据解析
- (void)JSOn{
    
    
    HUD = [[MBProgressHUD alloc] initWithView:self.tableView];
    [self.tableView addSubview:HUD];
    HUD.labelText = @"正在加载";
    [HUD show:YES];
    
    CATransition * animation = [CATransition animation];
    animation.duration = 0.6;
    animation.type = @"cube";
    
    
    
    animation.subtype = kCATransitionFromRight;
    [self.tableView.layer addAnimation:animation forKey:@"过度动画"];
    if (!_uuu) {
        _uuu = [NSString stringWithFormat:PetRankingQI,[NSString stringWithFormat:@"%d",_a]];
    }
   
    
    
    NSURL * url = [NSURL URLWithString:_uuu];
    
    NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60.0];
    
    __weak MengwTableViewController * MTVC = self;
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data == nil) {
            return;
        }
        
        NSMutableDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray * arr1 = dic[@"data"];
        for (NSDictionary * modelDic in arr1) {
            HomePage * homePage = [[HomePage alloc] init];
            [homePage setValuesForKeysWithDictionary:modelDic];
            [MTVC.allDataArray addObject:homePage];
        }
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    //清除缓存
    SDImageCache * sdI = [SDImageCache sharedImageCache];
    [sdI clearDisk];
    [sdI clearMemory];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (_allDataArray.count != 0) {
        return _allDataArray.count;
    }else{
        return 10;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MPTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mengCell" forIndexPath:indexPath];
    if (_allDataArray.count != 0){
    cell.numberLable.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
    HomePage * homePage = _allDataArray[indexPath.row];
    cell.homePage = homePage;
        //HUD = nil;
        [MBProgressHUD hideHUDForView:self.tableView animated:YES];
    }
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MengwDetailsTableViewController * MDTVC = [[MengwDetailsTableViewController alloc] init];
    HomePage * homePage = _allDataArray[indexPath.row];
    MDTVC.ppid = homePage.ppid;
    [self.navigationController pushViewController:MDTVC animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
